# Maintainer: David Runge <dvzrv@archlinux.org>

pkgname=nlohmann-json
# upstream doesn't use annotated tags
_commit=bc889afb4c5bf1c0d8ee29ef35eaaf4c8bef8a5d  # v3.11.2^{}
pkgver=3.11.2
pkgrel=2
# tests are tracked in a separate repo
# https://github.com/nlohmann/json_test_data
_test_pkgver=3.1.0
pkgdesc="JSON for Modern C++"
arch=(any)
url="https://github.com/nlohmann/json"
license=(MIT)
makedepends=(cmake git)
source=(
  $pkgname::git+https://github.com/nlohmann/json#commit=$_commit?signed
  json_test_data-$_test_pkgver.tar.gz::https://github.com/nlohmann/json_test_data/archive/v$_test_pkgver.tar.gz
)
sha512sums=('SKIP'
            'db6c411b37f2154f5dd1ed90f4e8fa0907f4a736cd0ff79943bcacf9da422285ff142bb6a7dc6022b236090083166ac1ab197be3f480d8dc50b26a91a9477821')
b2sums=('SKIP'
        '809be0728a0b9d007fcc752911bdf6f7e548d6e3ec59871ea2b16d87d8248ca4dd2f681a1d0f82c618463294188ad41d6d965b8bdc39c70fdcf4b939d4121e9c')
validpgpkeys=('797167AE41C0A6D9232E48457F3CEA63AE251B69') # Niels Lohmann <mail@nlohmann.me>

pkgver() {
  cd $pkgname
  git describe --tags | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/v//g'
}

prepare() {
  (
    cd $pkgname
    # fix issues with gcc >= 13: https://github.com/nlohmann/json/issues/3927
    git cherry-pick -n 660d0b58565073975d6f5d94365d6cbf150a4cf8 6cec5aefc97ad219b6fd5a4132f88f7c8f6800ee
  )
  mkdir -vp build-test/
  mv -v json_test_data-${_test_pkgver}/ build-test/json_test_data/
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_INSTALL_LIBDIR=/usr/lib
    -D JSON_MultipleHeaders=ON
    -S $pkgname
    -W no-dev
  )
  local cmake_test_options=(
    "${cmake_options[@]}"
    -D BUILD_TESTING=ON
    -D JSON_BuildTests=ON
    -B build-test
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose

  cmake "${cmake_test_options[@]}"
  cmake --build build-test --verbose
}

check() {
  ctest --test-dir build-test --output-on-failure
}

package() {
  DESTDIR="$pkgdir" cmake --install build
  install -vDm 644 $pkgname/README.md -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 $pkgname/LICENSE.MIT -t "$pkgdir/usr/share/licenses/$pkgname/"
}
